//
//  TouchView.swift
//  SwiftDemo
//
//  Created by Jason on 15/5/27.
//  Copyright (c) 2015年 Jason Jiang. All rights reserved.
//

import UIKit


protocol TouchViewDelegate:NSObjectProtocol{
    
     func viewTouched(touchView : TouchView)
    
}

class TouchView: UIView {
    
    var btn:UIButton = UIButton()
    weak var delegate:TouchViewDelegate!;
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        self.initSubViews()
        ;
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    func initSubViews(){
        
        btn.frame = CGRectMake(150, 0, 60, 40);
        btn.backgroundColor = UIColor.redColor();
        btn.setTitle("按鈕", forState: UIControlState.Normal)
        btn.addTarget(self, action:"btnAction:", forControlEvents: UIControlEvents.TouchUpInside);
        self.addSubview(btn);
    }
    
    
    func btnAction(btn:UIButton){
        
        print("hello world")
        let canDo :Bool = delegate!.respondsToSelector(Selector("viewTouched:"));
        
        if(canDo){
            
            delegate?.viewTouched(self);
            
        }else{
            
            NSLog("fsdfsd");
        }
        
    }

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

    
    
}
